<?php
// 如果是post请求，就执行里面的代码
if($_SERVER['REQUEST_METHOD']=="POST") {
	$title = $_POST['title'];
	$content = $_POST['content'];
	
	// 文章内容添加到数据库
	include_once("../pdo.php");
	
	$sql = "insert into articles(title, content) values(:title, :content)";
	
	// 预处理sql语句
	$stmt = $pdo->prepare($sql);
	
	// 绑定参数
	$stmt->bindParam(":title", $title);
	$stmt->bindParam(":content", $content);
	
	// 执行sql语句
	$stmt->execute();
	
	// 返回是否添加成功
	if($stmt->rowCount()>0){
		header("Location: list.php");
		exit();
	} else{
		die("添加失败");
	}
}
?>

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Hugo 0.88.1">
    <title>博客后台</title>

    <!-- Bootstrap core CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet">

<meta name="theme-color" content="#7952b3">


    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        user-select: none;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>

    
    <!-- Custom styles for this template -->
    <link href="./css/dashboard.css" rel="stylesheet">
  </head>
  <body>
    
<header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
  <a class="navbar-brand col-md-3 col-lg-2 me-0 px-3" href="#">博客后台</a>
  <button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
  <div class="navbar-nav">
    <div class="nav-item text-nowrap">
      <a class="nav-link px-3" href="#">退出</a>
    </div>
  </div>
</header>

<div class="container-fluid">
  <div class="row">
    <nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
      <div class="position-sticky pt-3">
	  <ul class="nav flex-column">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="./list.php">
              <span data-feather="home"></span>
              文章列表
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="./add.php">
              <span data-feather="file"></span>
              添加文章
            </a>
          </li>
         
        </ul>
      </div>
    </nav>

    <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
      <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">文章列表</h1>
      </div>

	  <form action="" method="post">
		<div>文章标题：</div>
		<div><input type="text" class="form-control" name="title" placeholder="请输入文章标题"></div>
		<textarea name="content" hidden="hidden" id="text1" cols="30" rows="10"></textarea>
		<div>文章内容：</div>
		<div id="editor"></div>
		<div><input type="submit"  class="btn btn-primary" style="margin-top:10px" value="发布博客"></div>
	</form>
	
    </main>
  </div>
</div>


    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/js/bootstrap.bundle.min.js"></script>
	
      <script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js" integrity="sha384-uO3SXW5IuS1ZpFPKugNNWqTZRRglnUJK6UAZ/gxOX80nxEkN9NcGZTftn6RzhGWE" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js" integrity="sha384-zNy6FEbO50N+Cg5wap8IKA4M/ZnLJgzc6w2NqACZaK0u0FXfOWRRJOnQtpZun8ha" crossorigin="anonymous"></script>
	  <script src="./js/dashboard.js"></script>

	  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://unpkg.com/wangeditor/dist/wangEditor.min.js"></script>
	<script>
		const editor = new window.wangEditor("#editor");
		
		const $text1 = $('#text1')
		editor.config.onchange = function (html) {
			// 第二步，监控变化，同步更新到 textarea
			$text1.val(html)
		}
		editor.create();
	</script>


  </body>
</html>


